package com.atwulidun.sort;

import java.util.Arrays;

public class AllSorts {
    public static void main(String[] args) {
        int[] arr = {-1, -33, 5, 3, 44, 3, 26, 1, 6, 7, -21};
        selectSort(arr);
        System.out.println("排序完后为：");
        System.out.println(Arrays.toString(arr));
    }

    // 冒泡排序
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            boolean flag = false;
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
    }

    // 快速排序
    public static void quickSort(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int l = left, r = right, base = arr[left];
        while (l != r) {
            while (l < r && arr[r] >= base) {
                r--;
            }
            while (l < r && arr[l] <= base) {
                l++;
            }
            int temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
        }
        arr[left] = arr[l];
        arr[l] = base;
        quickSort(arr, left, l - 1);
        quickSort(arr, r + 1, right);
    }

    // 插入排序
    public static void insertSortI(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int insertVal = arr[i];
            int insertIdx = i - 1;
            while (insertIdx >= 0 && insertVal < arr[insertIdx]) {
                arr[insertIdx + 1] = arr[insertIdx];
                insertIdx--;
            }
            if (insertIdx + 1 != i) {
                arr[insertIdx + 1] = insertVal;
            }
        }
    }

    public static void insertSortII(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            for (int j = i - 1; j >= 0; j--) {
                if (arr[j + 1] < arr[j]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    // 归并排序
    // 合并方法
    private static void merge(int[] arr, int left, int mid, int right) {
        int[] temp = new int[arr.length];
        int l = left, r = mid + 1, t = 0;
        while (l <= mid && r <= right) {
            if (arr[l] < arr[r]) {
                temp[t++] = arr[l++];
            } else {
                temp[t++] = arr[r++];
            }
        }
        while (l <= mid) {
            temp[t++] = arr[l++];
        }
        while (r <= right) {
            temp[t++] = arr[r++];
        }
        l = left;
        t = 0;
        while (l <= right) {
            arr[l++] = temp[t++];
        }
    }

    // 递归方式实现归并排序
    public static void recursiveMergeSort(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = left + (right - left) / 2;
        recursiveMergeSort(arr, left, mid);
        recursiveMergeSort(arr, mid + 1, right);
        merge(arr, left, mid, right);
    }

    // 迭代方式实现归并排序
    public static void iterativeMergeSort(int[] arr) {
        // 外层循环控制步长
        for (int i = 1; i < arr.length; i *= 2) {
            int step = 2 * i;
            for (int j = 0; j < arr.length; j += step) {
                merge(arr, j, Math.min(arr.length - 1, j + (j + step - 1 - j) / 2), Math.min(arr.length - 1, j + step - 1));
            }
        }
    }

    // 堆排序
    // 调整堆的方法
    private static void adjustHeap(int[] arr, int i, int length) {
        int temp = arr[i];
        for (int k = 2 * i + 1; k < length; k = 2 * k + 1) {
            if ((k + 1) < length && arr[k + 1] > arr[k]) {
                k++;
            }
            if (arr[k] > temp) {
                arr[i] = arr[k];
                i = k;
            } else {
                break;
            }
        }
        arr[i] = temp;
    }

    // 堆排序
    public static void heapSort(int[] arr) {
        // 由于要升序排序，因此调整得到一个大顶堆
        for (int i = arr.length / 2 - 1; i >= 0; i--) {
            adjustHeap(arr, i, arr.length);
        }
        for (int i = arr.length - 1; i > 0; i--) {
            int temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;
            adjustHeap(arr, 0, i);
        }
    }

    // 希尔排序
    public static void shellSortI(int[] arr) {
        for (int i = arr.length / 2; i > 0; i /= 2) {
            for (int j = i; j < arr.length; j++) {
                for (int k = j - i; k >= 0; k -= i) {
                    if (arr[k + i] < arr[k]) {
                        int temp = arr[k];
                        arr[k] = arr[k + i];
                        arr[k + i] = temp;
                    }
                }
            }
        }
    }

    public static void shellSortII(int[] arr) {
        for (int i = arr.length / 2; i > 0; i /= 2) {
            for (int j = i; j < arr.length; j++) {
                int insertVal = arr[j];
                int insertIdx = j - i;
                while (insertIdx >= 0 && insertVal < arr[insertIdx]) {
                    arr[insertIdx + i] = arr[insertIdx];
                    insertIdx -= i;
                }
                if (insertIdx + i != j) {
                    arr[insertIdx + i] = insertVal;
                }
            }
        }
    }

    // 基数排序
    public static void redixSort(int[] arr) {
        int minNegative = 0;
        for (int i : arr) {
            if (i < 0 && i < minNegative) {
                minNegative = i;
            }
        }
        for (int i = 0; i < arr.length; i++) {
            arr[i] -= minNegative;
        }
        int maxPositive = -1;
        for (int i : arr) {
            if (i > maxPositive) {
                maxPositive = i;
            }
        }
        int times = (maxPositive + "").length();
        int[][] buckets = new int[10][arr.length];
        int[] bucketElementsCount = new int[10];
        for (int m = 0, b = 1; m < times; m++, b *= 10) {
            for (int k : arr) {
                int bucketNum = k / b % 10;
                buckets[bucketNum][bucketElementsCount[bucketNum]] = k;
                bucketElementsCount[bucketNum]++;
            }
            int index = 0;
            for (int i = 0; i < 10; i++) {
                if (bucketElementsCount[i] != 0) {
                    for (int j = 0; j < bucketElementsCount[i]; j++) {
                        arr[index++] = buckets[i][j];
                    }
                    bucketElementsCount[i] = 0;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            arr[i] += minNegative;
        }
    }

    // 选择排序
    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minIdx = i;
            int minVal = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < minVal) {
                    minVal = arr[j];
                    minIdx = j;
                }
            }
            if (minIdx != i) {
                arr[minIdx] = arr[i];
                arr[i] = minVal;
            }
        }
    }
}
